import static java.lang.Thread.sleep;

public class demo7 {
    public static void main(String[] args) throws InterruptedException {
        Object loc1 = new Object();
        Object loc2 = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (loc1) {
                try {
                    //loc1.wait();
                    sleep(1);
                    synchronized (loc2) {
                        loc2.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (loc2) {
                try {
                   // loc2.wait();
                    sleep(1);
                    synchronized (loc1) {
                        loc1.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();


    }
    public static void main1(String[] args) throws InterruptedException {
        Object loc1 = new Object();
        Object loc2 = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (loc1) {
                try {
                    loc1.wait();
                    synchronized (loc2) {
                        loc2.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
//        
        Thread t2 = new Thread(() -> {
            synchronized (loc2) {
                try {
                    loc2.wait();
                    synchronized (loc1) {
                        loc1.notify();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();

        sleep(1000);

        synchronized (loc1){
            loc1.notify();
        }

    }
}
